﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using XuanXiang.Infrastructure.Attr;
using XuanXiang.Infrastructure.Dto.Request;
using XuanXiang.Infrastructure.Dto.Request.Login;
using XuanXiang.Infrastructure.Dto.Request.Merchant;
using XuanXiang.Infrastructure.Dto.Request.Supplier;
using XuanXiang.Infrastructure.Dto.Response;
using XuanXiang.Infrastructure.Dto.Response.Supplier;
using XuanXiang.Infrastructure.EFCore.MySql;
using XuanXiang.Infrastructure.Tools;
using XuanXiang.Service.Interface;

namespace XuanXiang.Api.Admin.Controllers.Wechat
{
    
    [ApiGroup("Wechat(微信模块)")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WechatSupplierController : ControllerBase
    {
        private readonly I_Supplier_Service _supplier_Service;
        private readonly IConfiguration _configuration;
       

        public WechatSupplierController(IConfiguration configuration,
         
            I_Supplier_Service supplier_Service)
        {
            _supplier_Service = supplier_Service;
            _configuration = configuration;
            


        }

        /// <summary>
        /// 初始化供应商密码
        /// </summary>
        /// <returns></returns>
     /*   [HttpPost]
        public async Task<IActionResult> Init_User()
        {
            var result = await _login_Service.Init_User();
            return Ok(result);
        }*/


        /// <summary>
        /// 供应商登录
        /// {account/password}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<IActionResult> Supplier_Logins(Login_dto dto)
        {

            dto.code = Config.GUID2();
            var data = await _supplier_Service.Supplier_Logins(dto);
            if (data.code == Api_Code.ok)
            {
                var claims = new[]
                    {
                    new Claim(ClaimTypes.Name,  dto.code),
                     new Claim(ClaimTypes.Actor,  dto.account!),
                    new Claim(ClaimTypes.Role,"supplier")//角色设定
                };
                var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:SecurityKey"]));
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                string jwtKey = _configuration["JWT:issuer"];
                var token = new JwtSecurityToken(
                    issuer: jwtKey,
                    audience: jwtKey,
                    claims: claims,
                    expires: DateTime.Now.AddMinutes(60 * 12), //token 过期时间
                    signingCredentials: creds);

                data.data = new
                {

                    token = new JwtSecurityTokenHandler().WriteToken(token)
                };

            }

            return Ok(data);
        }

        /// <summary>
        /// 获取当前登录的供应商
        /// </summary>
        /// <returns></returns>
        private Supplier_Dto Get_Supplier()
        {
            var code = Response.HttpContext.User.Identity?.Name;

            return _supplier_Service.Check_Supplier_Login(code!,"");
        }

        /// <summary>
        /// 检查供应商登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles ="supplier")]
        public async Task<IActionResult> Check_Supplier_Login()
        {
            await Task.Delay(1);
            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }
            return Ok(new Api_Result_Dto()
            {
                code = 0,
                message = "ok",
                data = supplier
            });
        }

        /// <summary>
        /// 获取供应商的订单
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="type"></param>
        /// <returns></returns>

        [HttpGet]
        [Authorize(Roles = "supplier")]
        public async Task<IActionResult> Get_Supplier_Order(int page=1,int limit=10,int type=1)
        {
            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }
            var result = await _supplier_Service.Get_Supplier_Order(new Fenye_Dto() { 
            page = page,
            limit = limit,
            type = type,
            id=supplier.id
            });
        
            return Ok(result);
        }


        /// <summary>
        /// 获取供应商所有的类别和类别下的商品
        /// </summary>
        /// <param name="isBan"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Supplier_AllType(string? id) {
            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }
            var result = await _supplier_Service.Get_Supplier_AllType(new Fenye_Dto() { 
            id=id==null?supplier.id:id,
            });
            return Ok(result);
        }


        /// <summary>
        /// 供应商订单-确认/拒绝接单
        /// {id,isAccept}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_AcceptOrder(IsBan_Dto dto) {
            var result = await _supplier_Service.Supplier_AcceptOrder(dto);
            return Ok(result);
        }

        /// <summary>
        /// 供应商订单-发货/取消发货
        /// {id,isSend}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_SendOrder(IsBan_Dto dto) {
            var result = await _supplier_Service.Supplier_SendOrder(dto);
            return Ok(result);
        }

        /// <summary>
        /// 供应商订单-取消订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_CancelOrder(IsBan_Dto dto) {
            var result = await _supplier_Service.Supplier_CancelOrder(dto);
            return Ok(result);
        }

        /// <summary>
        /// 供应商商品-上下架商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_isShelveGoods(IsBan_Dto dto) {
            var result = await _supplier_Service.Supplier_isShelveGoods(dto);
            return Ok(result);
        }


        /// <summary>
        /// 供应商-新增商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_AddGoods(Supplier_AddGoods_Dto dto) 
        {

            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }
            dto.sid = supplier.id;
            
            var result = await _supplier_Service.Supplier_AddGoods(dto);
            return Ok(result);


        }

        /// <summary>
        /// 供应商-设置活动商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_SetPromoteGoods(Supplier_SetPromoteGoods_Dto dto) {
            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }
            dto.sid = supplier.id;

            var result = await _supplier_Service.Supplier_SetPromoteGoods(dto);
            return Ok(result);
        }


       
        /// <summary>
        /// 供应商-取消商品折扣
        /// {gid}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_CancelPromoteGoods(Supplier_SetPromoteGoods_Dto dto)
        {
            var result = await _supplier_Service.Supplier_CancelPromoteGoods(dto.gid);
            return Ok(result);
        }

        /// <summary>
        /// 供应商-商品补货
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_AddInventory(Supplier_AddInventory_Dto dto) {
            var result = await _supplier_Service.Supplier_AddInventory(dto);
            return Ok(result);
        }

        /// <summary>
        /// 供应商-新增商品类别
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_AddType(Supplier_AddType_Dto dto) {
            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }
            dto.sid = supplier.id;

            var result = await _supplier_Service.Supplier_AddType(dto);
            return Ok(result);
        }


        /// <summary>
        /// 供应商-编辑商品类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_UpdateGoodsType(Supplier_Update_Type_Dto dto) {
            var result = await _supplier_Service.Supplier_UpdateGoodsType(dto);
            return Ok(result);
        }

        /// <summary>
        /// 供应商-删除商品类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Supplier_DeleteGoodsType(Delete_Dto dto) {
            var result = await _supplier_Service.Supplier_DeleteGoodsType(dto.id);
            return Ok(result);
        }


        /// <summary>
        /// 供应商-获取我的订单数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Supplier_GetOrderData() {
            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }

            var result = await _supplier_Service.Supplier_GetOrderData(supplier.id);
            return Ok(result);
        }

        /// <summary>
        /// 供应商-获取销售数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Supplier_GetSalesData() {
            var supplier = this.Get_Supplier();
            if (supplier == null)
            {
                return Unauthorized();//401
            }

            var result = await _supplier_Service.Supplier_GetSalesData(supplier.id);
            return Ok(result);
        }


        /// <summary>
        /// 供应商-获取指定日期订单数据
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Supplier_GetDateOrderData(DateTime? startTime,DateTime? endTime) {
            var result = await _supplier_Service.Supplier_GetDateOrderData(startTime,endTime);
            return Ok(result);
        }
    }
}

